<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <el-row>
        <el-col :span="15">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>

      <el-row>
        <el-form :model="queryParams" ref="queryRef" label-width="100">
          <el-row>
            <el-col :span="9">
              <el-form-item label="仪器名称：" prop="name" label-width="80">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.name"
                  placeholder="请输入仪器名称"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>

            <el-col :span="9">
              <el-form-item label="仪器编号：" prop="number">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.number"
                  placeholder="请输入"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-row justify="end">
                <el-button type="success" @click.enter="handleQuery"
                  >搜索</el-button
                >
                <el-button class="resetBtn" @click="resetQuery">重置</el-button>
              </el-row>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="9">
              <el-form-item label="仪器型号：" prop="code" label-width="80">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.code"
                  placeholder="请输入仪器型号"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>

            <el-col :span="15">
              <el-form-item label="下次检定日期：" prop="typeId">
                <el-date-picker
                  value-format="YYYY-MM-DD"
                  type="daterange"
                  v-model="dateRange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-row>
      <div>
        <el-row>
            <p class="pinformation">仪器设备列表</p>
            <p class="totalStyle">共 {{ total }} 条数据</p>
        </el-row>
        <el-table
          ref="mainTableRef"
          class="business"
          v-loading="loading"
          :data="instrumentTable"
          :row-class-name="tableRowClass"
          @row-click="rowClick"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="仪器名称" align="center" prop="name" />
          <el-table-column label="仪器编号" align="center" prop="number" />
          <el-table-column label="仪器型号" align="center" prop="code" />
          <el-table-column label="下次检定日期" align="center" prop="checkAdjustDate" />
          <el-table-column label="检定日期" align="center" prop="adjustDate" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-card class="rightCard">
      <!-- 搜索条件区域 -->
      <el-row>
        <el-col :span="8">
          <el-form-item label="仪器名称：" prop="name">
            {{ searchOutinForm.name }}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="仪器型号：" prop="code">
            {{ searchOutinForm.code }}
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="仪器编号：" prop="number">
            {{ searchOutinForm.number }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form
        label-width="120px"
        :model="searchOutinForm"
        ref="searchOutinRef"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="状态：" prop="stepId">
              <el-select
                v-model="searchOutinForm.stepId"
                placeholder="请选择记录状态"
                style="width: 100%"
                filterable
                clearable
              >
                <el-option
                  v-for="item in proxy.addListNoLimit(sys_instrument_step)"
                  :key="item.value"
                  :label="item.label"
                  :value="parseInt(item.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检定日期：">
              <el-date-picker
                v-model="dateRange1"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="下次检定日期：">
              <el-date-picker
                v-model="dateRange2"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="检定证书编号：" prop="adjustCertificateNumber">
              <el-input  clearable maxlength="100"
                v-model="searchOutinForm.adjustCertificateNumber"
                placeholder="请输入检定证书编号"
                @keyup.enter="getListAdjust"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检定单位：" prop="adjustOrgName">
              <el-input  clearable maxlength="100" v-model="searchOutinForm.adjustOrgName" @keyup.enter="getListAdjust" placeholder="请输入检定单位"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-row justify="end">
              <el-button type="success" @click="getListAdjust">搜索</el-button>
              <el-button class="resetBtn" @click="resetBtn">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <!-- /搜索条件区域 -->

      <!-- 技术指标表格 -->
      <el-row>
          <p class="pinformation">检定校准记录</p>
          <p class="totalStyle">共 {{ OtinTotal }} 条数据</p>
          <el-row style="padding-bottom: 5px; margin-left: 20px;">
            <el-button link icon="Plus">新增</el-button>
          </el-row>
      </el-row>

      <el-table
        ref="sub1TableRef"
        class="business"
        v-loading="targetTableLoading"
        :highlight-current-row="highLightRow"
        :data="targetList"
        @selection-change="outSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="检定日期" align="center" prop="adjustDate" />
        <el-table-column label="检定单位" align="center" prop="adjustOrgName" />
        <el-table-column
          label="检定周期（月）"
          align="center"
          prop="adjustCycle"
        />
        <el-table-column
          label="检定证书编号"
          align="center"
          prop="adjustCertificateNumber"
        />
        <el-table-column
          label="下次检定日期"
          align="center"
          prop="checkAdjustDate"
        />
        <el-table-column label="状态" align="center" prop="stepId">
          <template #default="scope">
            <dict-tag :options="sys_instrument_step" :value="scope.row.stepId" />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="success" @click="delAdjustClick(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- /技术指标表格 -->
      <pagination
        v-show="OtinTotal > 0"
        :total="OtinTotal"
        v-model:page="searchOutinForm.pageNum"
        v-model:limit="searchOutinForm.pageSize"
      />
      <!-- @pagination="" -->
    </el-card>
  </div>
</template>

<script setup name="Instrument">
import { listInstrument } from "@/api/resource/instrument";
import { listAdjust, formAdjust, delAdjust } from "@/api/resource/adjust";

const { proxy } = getCurrentInstance();
const { sys_instrument_step } = proxy.useDict(
  "sys_instrument_step"
);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const instrumentTable = ref([]);
const highLightRow = ref(true);
const targetTableLoading = ref(false);
const dateRange = ref([]);
const dateRange1 = ref([]);
const dateRange2 = ref([]);
const searchOutinForm = ref({
  pageNum: 1,
  pageSize: 10,
  stepId: -1
});
onMounted(() => {
  nextTick(() => {
    getList();
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 73 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 73 },
  { tableName: "sub1", bottomHeight: 73 },
]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仪器档案列表 */
function getList() {
  loading.value = true;
  listInstrument(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      instrumentTable.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.instrumentId);
}

const outIds = ref([]);
function outSelectionChange(selection) {
  outIds.value = selection.map((item) => item.outinId);
}

const OtinTotal = ref(0);

const targetList = ref([]);

function resetBtn() {
  proxy.resetForm("searchOutinRef");
  dateRange1.value = [];
  dateRange2.value = [];
  getListAdjust();
}

function rowClick(row) {
  proxy.resetForm("searchOutinRef");
  dateRange1.value = [];
  dateRange2.value = [];
  formAdjust(row.instrumentId).then((res) => {
    searchOutinForm.value.name = res.data.name;
    searchOutinForm.value.code = res.data.code;
    searchOutinForm.value.number = res.data.number;
    searchOutinForm.value.instrumentId = res.data.instrumentId;
    heightBright.value = row.instrumentId
    getListAdjust();
  });
}

function getListAdjust() {
  if (searchOutinForm.value.instrumentId) {
    searchOutinForm.value.params = {
      beginTime: dateRange1.value[0],
      endTime: dateRange1.value[1],
      beginTime1: dateRange2.value[0],
      endTime1: dateRange2.value[1],
    };
    listAdjust(searchOutinForm.value).then((res) => {
      targetList.value = res.rows;
      OtinTotal.value = res.total;
    });
  } else {
    proxy.$modal.msgError("请先选择要搜索的仪器设备");
  }
}

function delAdjustClick(row) {
  proxy.$modal
    .confirm("是否确认删除该条记录？")
    .then(function () {
      delAdjust(row.adjustId).then((res) => {
        proxy.$modal.msgSuccess("删除成功");
        getListAdjust();
      });
    })
    .catch(() => {});
}

const heightBright = ref("")
function tableRowClass({ row }) {
  if (row) {
    if (row.instrumentId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}
</script>

<style lang="scss" scoped></style>
